function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
%      UNTITLED, by itself, creates a new UNTITLED or raises the existing
%      singleton*.
%
%      H = UNTITLED returns the handle to a new UNTITLED or the handle to
%      the existing singleton*.
%
%      UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED.M with the given input arguments.
%
%      UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help untitled

% Last Modified by GUIDE v2.5 03-Apr-2013 23:34:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @untitled_OpeningFcn, ...
                   'gui_OutputFcn',  @untitled_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to untitled (see VARARGIN)

% Choose default command line output for untitled
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in imgSelect.
set(handles.imgSelect,'cdata',imgSelectButton);
function imgSelect = imgSelectButton

imgSelect = iconize(imread('openFolder.png'));
imgSelect(imgSelect==0) = 4.8*255;

function out = iconize(a)

% Find the size of the acquired image and determine how much data will need
% to be lost in order to form a 18x18 icon
[r,c,d] = size(a);
r_skip = ceil(r/45);
c_skip = ceil(c/45);

% Create the 18x18 icon (RGB data)
out = a(1:r_skip:end,1:c_skip:end,:);

function imgSelect_Callback(hObject, eventdata, handles)
% hObject    handle to imgSelect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.originalImg);
[filename, pathname] = uigetfile('*.bmp','Select an image file');
img = imread(fullfile(pathname,filename));
set(handles.imgLink,'String',fullfile(pathname,filename));
handles.image = img;
guidata(hObject,handles);
imshow(img);


% --- Executes on button press in imgCompress.
function imgCompress_Callback(hObject, eventdata, handles)
% hObject    handle to imgCompress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~exist('imageFile', 'var')
    imageFile = 'lena.bmp';
end

if ~exist('transforms', 'var')
    transforms = {'JPEG','JP2'};
end

A = handles.image;
[N,M] = size(A);
peak = 255;
res = cell(numel(transforms),1);

for tno = 1:numel(transforms)
    
    if strcmpi(transforms{tno}, 'JP2')
        if verLessThan('matlab', '7.11.0')
            disp(' ');
            disp('Matlab imwrite with JP2 option not available in this version.');
            if strcmpi(imageFile, 'lena.bmp')
                disp('Results for lena.bmp from another computer is used.');
                res{tno} = struct('bits',[26208, 33464, 50968, 64032, 80240, 100064, ...
                    126728, 157728, 192664, 217072, 257504, 313824, 388160, 452240, ...
                    519904, 595472, 695344], ...
                    'PSNR',[31.0695, 32.1500, 34.0647, 35.1917, 36.2943, 37.2754, ...
                    38.4545, 39.5063, 40.3786, 41.0092, 41.8209, 42.8968, 44.0517, ...
                    45.0905, 46.3231, 47.7135, 49.3444]  );
            elseif strcmpi(imageFile, 'barbara.bmp')
                disp('Results for barbara.bmp from another computer is used.');
                res{tno} = struct('bits',[26880, 32936, 50968, 64040, 79896, ...
                    101336, 127568, 157512, 192248, 215728, 258384, 314192, ...
                    389488, 452112, 520808, 592416, 692440], ...
                    'PSNR',[24.7801, 25.4151, 27.1745, 28.2991, 29.3373, 30.6678, ...
                    32.0822, 33.3990, 34.7408, 35.5528, 37.0757, 38.5875, 40.4192, ...
                    41.7770, 43.0677, 44.2891, 46.2447] );
            elseif strcmpi(imageFile, 'boat.bmp')
                disp('Results for boat.bmp from another computer is used.');
                res{tno} = struct('bits',[26872, 33168, 50392, 64008, 80288, ...
                    101384, 127536, 156256, 191208, 216824, 257328, 313552, ...
                    387936, 451736, 520304, 592488, 694032], ...
                    'PSNR',[26.6568, 27.4076, 29.0232, 30.0227, 30.9728, ...
                    32.1109, 33.2005, 34.1659, 35.1261, 35.7873, 36.6386, ...
                    37.7957, 39.0926, 40.4254, 41.9366, 43.3093, 45.1097]  );
            else
                res{tno} = struct('bits',8*numel(A), 'PSNR', inf);
            end
        else
            disp(' ');
            disp('Use Matlab imwrite with JP2 option.');
            CR = [80 60 40 32 25 20 16 13 10.67 9.5 8 6.6 5.33 4.6 4 3.5 3];
            res{tno} = struct('bits',zeros(1,numel(CR)), 'PSNR',zeros(1,numel(CR)) );
            i = 0;
            for cr = CR
                i = i+1;
                imwrite(uint8(A+128),'tempfile.jp2','jp2','CompressionRatio',cr);
                h = dir('tempfile.jp2');
                res{tno}.bits(i) = (h.bytes)*8;  % bits
                Ar = double(imread('tempfile.jp2'));
                R = double(A+128)-Ar;
                res{tno}.PSNR(i) = 10*log10(((numel(A)*peak^2)/sum(sum(R.*R))));
            end
            I1 =  double(imread('tempfile.jp2'));
            axes(handles.jpeg2000);
%             imwrite(I1,'filename.jp2')
%             info=imfinfo('filename.jp2');
%             str = sprintf('1) %shello \n\n 2) fuck you %s ',info.Filename,info.Format); 
%             set(handles.jpeg2000Info,'String',str);
            imshow(I1);
        end
        
    elseif strcmpi(transforms{tno}, 'JPEG')
        disp(' ');
        disp('Use Matlab imwrite with JPEG option.');
        quality = [10:5:90,93];
        res{tno} = struct('bits',zeros(1,numel(quality)), 'PSNR',zeros(1,numel(quality)) );
        i = 0;
        for q = quality
            i = i+1;
            imwrite(uint8(A+128),'tempfile.jpg','JPEG','Quality',q);
            h = dir('tempfile.jpg');
            res{tno}.bits(i) = (h.bytes)*8;  % bits
            Ar = double(imread('tempfile.jpg'));
            R = double(A+128)-Ar;
            res{tno}.PSNR(i) = 10*log10(((numel(A)*peak^2)/sum(sum(R.*R))));
        end
        I2=double(imread('tempfile.jpg'));
        axes(handles.jpeg);
        %imwrite(I2,'filename2.jpg')
        imshow(I2);
    end
end
